// const app = require('express')()

//导入uuid模块
const uuid = require('node-uuid')

//导入数据库操作模块
const db = require("../../db/index")


/**
 * 获取学习视频列表
 * @param {*} req 
 * @param {*} res 
 */
exports.getTagsList = async (req, res) => {
    // console.log('获取学习视频列表'+req.ip)
    let type = req.query.type
    console.log('type，' + type)
    if (!type || type == "" || type == undefined) {
        return res.send({
            code: 505,
            msg: '请指定标签type',
            data: {}
        })
    }
    const sql = `SELECT DISTINCT tag_name FROM xks_tags WHERE tag_type = ?`;
    await db.query(sql, type, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '发送了错误' + err.message,
                data: {}
            })
        }

        if (!results.length > 0) {
            return res.send({
                code: 400,
                msg: '没有找到数据',
                data: {}
            })
        }

        return res.send({
            code: 200,
            msg: '成功',
            data: results
        })
    })

}

/**
 * 获取标签图标
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.getTagsIcon = async (req, res) => {
    const type = req.query.type

    let sql = ''
    if (!type || type == undefined || type == '') {
        sql = `SELECT DISTINCT tag_name,icon_url FROM xks_tags_icon WHERE status != 0 `;
        await db.query(sql, (err, results) => {
            if (err) {
                return res.send({
                    code: 505,
                    msg: '发送了错误' + err.message,
                    data: {}
                })
            }

            if (!results.length > 0) {
                return res.send({
                    code: 400,
                    msg: '没有找到数据',
                    data: {}
                })
            }

            return res.send({
                code: 200,
                msg: '成功',
                data: results
            })
        })
    }
    else {
        sql = `SELECT DISTINCT tags.tag_name,icon.icon_url FROM xks_tags AS tags , xks_tags_icon AS icon WHERE tags.tag_name = icon.tag_name AND tags.tag_type = ?`;
        await db.query(sql, type, (err, results) => {
            if (err) {
                return res.send({
                    code: 505,
                    msg: '发送了错误' + err.message,
                    data: {}
                })
            }

            if (!results.length > 0) {
                return res.send({
                    code: 400,
                    msg: '没有找到数据',
                    data: {}
                })
            }

            return res.send({
                code: 200,
                msg: '成功',
                data: results
            })
        })
    }
}


